package cn.kust.clock.controller;

import cn.kust.clock.pojo.dto.TravelsOverDTO;
import cn.kust.clock.pojo.vo.TravelsDetailVO;
import cn.kust.clock.pojo.vo.TravelsVO;
import cn.kust.clock.service.impl.TravelsServiceImpl;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author bjs
 * 44·14@date 2022/7/27 0027  15:28
 **/

@RestController
@RequestMapping("/api-clock/travels")
public class TravelsController {
    @Autowired
    private TravelsServiceImpl travelsService;

    /**
     *
     * @param page 传参页数
     * @return  游记的卡片列表
     * 测试通过
     */
    @GetMapping("/list/{page}")
    public ResponseResult<List<TravelsVO>> travelsList(@PathVariable("page") Integer page){
        try {
            //1.调用service层去获取数据
            List<TravelsVO> travelsVOList = travelsService.travelsList(page);
            //2.将返回数据封装
            return ResponseResult.success(travelsVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }

    /**
     *
     * @param id 游记的id
     * @return 游记的详细信息
     * 测试通过
     */
    @GetMapping("/travels/{id}")
    public ResponseResult<TravelsDetailVO> travels(@PathVariable("id") Long id){
        try {
            //1.调用service层去获取数据
            TravelsDetailVO travelsDetailVO = travelsService.travels(id);
            //2.将返回数据封装
            return ResponseResult.success(travelsDetailVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }

    /**
     *
     * @param userId 用户id
     * @return 用户的游记列表
     * 测试通过
     */
    @GetMapping("/myself/{userId}")
    public ResponseResult<List<TravelsVO>> myselfTravelsList(@PathVariable("userId") Long userId){
        try {
            //1.调用service层去获取数据
            List<TravelsVO> travelsVOList = travelsService.myselfTravelsList(userId);
            //2.将返回数据封装
            return ResponseResult.success(travelsVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }

    /**
     *
     * @param userId 用户id
     * @return 用户游记收藏列表
     * 测试通过
     */
    @GetMapping("/collection/{userId}")
    public ResponseResult<List<TravelsVO>> collectTravelsList(@PathVariable("userId") Long userId){
        //1.调用service层去获取数据
        try {
            List<TravelsVO> travelsVOList = travelsService.collectTravelsList(userId);
            //2.将返回数据封装
            return ResponseResult.success(travelsVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }
    /**
     *
     * @param userId 用户id
     * @return 游记的id
     * 测试通过
     */
    @GetMapping("/begin/{userId}")
    public ResponseResult<Long> begin(@PathVariable("userId") Long userId){
        try {
            //1.通过token获取用户信息
            //1.调用service层去获取数据
            Long travelsId = travelsService.begin(userId);
            //2.将返回数据封装
            return ResponseResult.success(travelsId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }

    /**
     *
     * @param travelsOverDTO 前端返回的数据
     * @return 请求是否成功
     *
     */
    @PostMapping("/over/{judge}")
    public ResponseResult Over(@RequestBody TravelsOverDTO travelsOverDTO,@PathVariable("judge") Integer judge){
        try {
            //1.调用service层去获取数据
            Integer over = travelsService.over(travelsOverDTO,judge);
            //2.将返回数据封装
            return ResponseResult.success(over);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(5000,"你的服务器出错");
        }
    }
}
